package edu.njust.cma.mapper;

import edu.njust.cma.pojo.Standard;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.jdbc.support.KeyHolder;

import java.util.List;

@Mapper
public interface StandardMapper {
    //1. 添加新的标准（添入标准库）
    @Insert("insert into standards (major_category_name, minor_category_name, number, name, standard_pdf_url," +
            " scope_limit, supplementary_explanation, creation_time) " +
            "values (#{majorCategoryName}, #{minorCategoryName}, #{number}, #{name}, #{standardPdfUrl}," +
            " #{scopeLimit}, #{supplementaryExplanation}, #{creationTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addStandard(Standard standard);

    @Select("select * from standards where id=#{standardId}")
    Standard getStandardById(Integer standardId);

    @Select("select * from standards")
    List<Standard> getAllStandard();

    @Select("select major_category_name from standards")
    List<String> getAllMajorCategoryName();

    @Select("select minor_category_name from standards where major_category_name=#{majorCategoryName}")
    List<String> getAllMinorCategoryName(String majorCategoryName);

    @Select("select number from standards where major_category_name=#{majorCategoryName} and minor_category_name=#{minorCategoryName}")
    List<String> getNumber(String majorCategoryName, String minorCategoryName);

    @Select("Select DISTINCT id from standards where number=#{number}")
    Integer getStandardId(String number);

    // 先根据标准id查询子项目，在根据子项目获取的ID检查所有的项目是否都完成了
    @Select("select count(*) from projects where id in (select project_id from small_project where standard_id=#{standardId}) and status=1")
    int isInProject(int standardId);

    @Delete("delete from standards where id=#{standardId}")
    void deleteStandardById(int standardId);
}
